package io.github.junxworks.qt.modules.tushare.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

import io.github.junxworks.qt.modules.data.service.DataStoreService;
import io.github.junxworks.qt.modules.tushare.Fields;
import io.github.junxworks.qt.modules.tushare.TushareAPIs;
import io.github.junxworks.qt.modules.tushare.entity.DStockHoldertrade;
import io.github.junxworks.qt.modules.tushare.entity.DStockRepurchase;
import io.github.junxworks.qt.modules.tushare.entity.DStockShareFloat;
import io.github.junxworks.qt.modules.tushare.entity.DStockToptenFloatholders;
import io.github.junxworks.qt.modules.tushare.entity.DStockToptenHolders;
import io.github.junxworks.qt.modules.tushare.mapper.OtherDataMapper;
import io.github.junxworks.qt.modules.tushare.service.TushareService;

@Service("TushareOtherDataService")
public class TushareOtherDataService extends TushareService {

	@Autowired
	private DataStoreService dataStoreService;

	@Autowired
	private OtherDataMapper otherDataMapper;

	/**
	 * 前十大股东
	 * @param endDate
	 */
	public void fetchStockTop10Holders(String endDate) {
		handleDataByLastFinanceReportPeriod(endDate, date -> {
			handleFetchStockTop10Holders(date);
		});
	}

	public void fetchStockTop10HoldersByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, date -> {
			handleFetchStockTop10Holders(date);
		});
	}

	private void handleFetchStockTop10Holders(String endDate) {
		Map<String, String> params = Maps.newHashMap();
		params.put("period", endDate);
		consumeTushareData(TushareAPIs.OTHER_TOP10_HOLDERS, Fields.F_TOP10_HOLDERS, DStockToptenHolders.class, params, l -> {
			if (!l.isEmpty()) {
				otherDataMapper.deleteStockTop10HoldersByEndDate(endDate);
				dataStoreService.insertBatch(l);
			}
		});
	}

	/**
	 * 前十大流通股东
	 * @param endDate
	 */
	public void fetchStockTop10Floatholders(String endDate) {
		handleDataByLastFinanceReportPeriod(endDate, date -> {
			handleFetchStockTop10Floatholders(date);
		});
	}

	public void fetchStockTop10FloatholdersByRange(String periodRange) {
		handleDataByLastFinanceReportPeriodRange(periodRange, date -> {
			handleFetchStockTop10Floatholders(date);
		});
	}

	private void handleFetchStockTop10Floatholders(String endDate) {
		Map<String, String> params = Maps.newHashMap();
		params.put("period", endDate);
		consumeTushareData(TushareAPIs.OTHER_TOP10_FLOATHOLDERS, Fields.F_TOP10_FLOATHOLDERS, DStockToptenFloatholders.class, params, l -> {
			if (!l.isEmpty()) {
				otherDataMapper.deleteStockTop10FloatholdersByEndDate(endDate);
				dataStoreService.insertBatch(l);
			}
		});
	}

	/**
	 * 回购数据抓取
	 * @param endDate
	 */
	public void fetchStockRepurchase(String annDate) {
		handleDataByDate(annDate, date -> {
			Map<String, String> params = Maps.newHashMap();
			params.put("ann_date", annDate);
			consumeTushareData(TushareAPIs.OTHER_REPURCHASE, Fields.F_REPURCHASE, DStockRepurchase.class, params, l -> {
				if (!l.isEmpty()) {
					otherDataMapper.deleteStockRepurchaseByAnnDate(annDate);
					dataStoreService.insertBatch(l);
				}
			});
		});
	}

	public void fetchStockRepurchaseByRange(String dateRange) {
		collectTradeDataByDateRange(dateRange, (d) -> fetchStockRepurchase(d));
	}

	/**
	 * 限售数据抓取
	 * @param endDate
	 */
	public void fetchStockShareFloat(String annDate) {
		handleDataByDate(annDate, date -> {
			Map<String, String> params = Maps.newHashMap();
			params.put("ann_date", annDate);
			consumeTushareData(TushareAPIs.OTHER_SHARE_FLOAT, Fields.F_SHARE_FLOAT, DStockShareFloat.class, params, l -> {
				if (!l.isEmpty()) {
					otherDataMapper.deleteStockShareFloatByAnnDate(annDate);
					dataStoreService.insertBatch(l);
				}
			});
		});
	}

	public void fetchStockShareFloatByRange(String dateRange) {
		collectTradeDataByDateRange(dateRange, (d) -> fetchStockShareFloat(d));
	}

	/**
	 * 股东增减持数据抓取
	 * @param endDate
	 */
	public void fetchStockHoldertrade(String annDate) {
		handleDataByDate(annDate, date -> {
			Map<String, String> params = Maps.newHashMap();
			params.put("ann_date", annDate);
			consumeTushareData(TushareAPIs.OTHER_STOCK_HOLDERTRADE, Fields.F_HOLDERTRADE, DStockHoldertrade.class, params, l -> {
				if (!l.isEmpty()) {
					otherDataMapper.deleteStockHoldertradeByAnnDate(annDate);
					dataStoreService.insertBatch(l);
				}
			});
		});
	}

	public void fetchStockHoldertradeByRange(String dateRange) {
		collectTradeDataByDateRange(dateRange, (d) -> fetchStockHoldertrade(d));
	}
}
